import dayjs from "../../../miniprogram_npm/dayjs/index"
Page({
  data: {
    //问题描述
    fileBiglist: '',
    //时间
    visitDate: "",
    minDate: Date.now(),
    maxDate: dayjs().add(2, 'day').valueOf(),
    currentDate: Date.now(),//当前时间作为默认时间
    //电话
    mobile: '',
    //选择获取的小屋
    selectHouseList: [],
    //选择维修项目
    selectRepairList: [],
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [
      { name: '北京西三旗花园1号楼 101' },
      { name: '北京东村家园3号楼 302' },
      { name: '北京育新花园3号楼 703' },
      { name: '北京天通苑北苑8号楼 403' },
    ],
    repairItem: [{ name: '水路卫浴' }, { name: '电路灯具' }, { name: '管道疏通' }, { name: '开锁换锁' }],
    fileList: [
      // { url: '/repair_pkg/static/uploads/attachment.jpg' },
      // { url: '/repair_pkg/static/uploads/attachment.jpg' },
    ],
  },

  //点击获取id
  onLoad({ id }) {
    // console.log(id);

    if (id) {
      wx.setNavigationBarTitle({ title: '修改报修信息' })
      this.getList(id)
    }
  },

  //删除图片
  del(e) {
    console.log('删除', e);
    const fileList = this.data.fileList
    fileList.splice(e.detail.index, 1)    //这样得到的是需要留下的部分
    // const fileList = this.data.fileList.splice(e.detail.index, 1)  获取的是需要删除的部分
    // console.log(fileList);
    this.setData({
      fileList
    })
  },

  //获取信息
  async getList(id) {
    const { data } = await wx.http.get('/repair/' + id)
    // console.log(data);
    this.setData({
      mobile: data.mobile,
      ['selectHouseList.name']: data.houseInfo,
      ['selectHouseList.id']: data.houseId,
      ['selectRepairList.name']: data.repairItemName,
      visitDate: data.appointment,
      fileBiglist: data.description,
      fileList: data.attachment,
      id: data.id
    })
  },

  //图片max-count="6"设置最大张数
  async afterRead(e) {
    // console.log(e.detail.file.url);
    try {
      const res = await wx.utils.upload(e.detail.file.url)
      // console.log(res);
      const { data } = res
      //获取图片
      const { fileList } = this.data
      console.log(data);
      fileList.push({ ...data })
      this.setData({
        fileList
      })
    } catch (error) {
      console.log(error);
    }
  },

  //选择获取的小屋
  selectHouse(e) {
    // console.log(e);
    this.setData({
      selectHouseList: e.detail
    })
    console.log(this.data.selectHouseList.id);
  },

  //选择维修项目
  selectRepairItem(e) {
    // console.log(e);
    this.setData({
      selectRepairList: e.detail
    })
    // console.log(this.data.selectRepairList);
  },

  //选择日期
  selectDate(e) {
    // console.log(e);
    this.setData({
      visitDate: wx.utils.formaTime(e.detail),
      dateLayerVisible: false
    })
  },

  async openHouseLayer() {
    // console.log('点击选择保修房屋');
    const res = await wx.http.get('/house')
    // console.log(res.data.map(item => item.name));
    this.setData({
      houseList: res.data
    })
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  //维修项目
  async openRepairLayer() {
    const { data } = await wx.http.get('/repairItem')
    // console.log(data);
    this.setData({
      repairLayerVisible: true,
      repairItem: data
    })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  async goList() {
    if (!this.data.selectHouseList.id) return wx.utils.message('请选择报修房屋')
    if (!this.data.visitDate) return wx.utils.message('请选择上门日期')
    if (!this.data.fileBiglist) return wx.utils.message('请输入问题描述')

    const data = {
      id: this.data.id,
      houseId: this.data.selectHouseList.id,
      repairItemId: this.data.selectRepairList.id,
      appointment: this.data.visitDate,
      mobile: this.data.mobile,
      description: this.data.fileBiglist,
      attachment: this.data.fileList

    }

    console.log(data);
    // 请求数据接口
    await wx.http.post('/repair', data)

    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },

})
